from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten, Reshape, Layer
from Xg_Utils.get_data import *
from Xg_Utils.process_data import *
import os
from Tested_models.sigle_layer_autoencoder.custom import Dense_dec
import keras.backend as K
import xgboost as xgb

#TODO: NO use

random_seed = 1225
experiment_name = 'boost_and_nn'
experiment_path = os.path.join('Xg_metadata', experiment_name).replace('\\', '/')
#os.mkdir(experiment_path)
nb_epochs = 10

nn_model = Sequential()
nn_model.add(Dense(500, input_shape=(1138,), activation='sigmoid'))
nn_model.add(Dropout(0.25))
nn_model.add(Dense(1, activation='sigmoid'))
nn_model.compile(optimizer='adam', loss='categorical_crossentropy')

params = {
    'booster': 'gbtree',
    # 'objective': 'binary:logistic',
    'early_stopping_rounds': 20,
    'scale_pos_weight': 1400.0 / 13458.0,
    # 'eval_metric': 'auc',
    'gamma': 0.1,
    'max_depth': 8,
    'lambda ': 550,
    'subsample': 0.7,
    'colsample_bytree': 0.4,
    'min_child_weight': 3,
    'eta': 0.02,
    'seed': random_seed,
}

print 'loading data'
train_xy = get_train_data(x_path='/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/DCP2P/train_x.csv',
                      y_path='/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/DCP2P/train_y.csv')
(X, y), (val_X, val_y) = random_split_train_val(train_xy)

for e in range(nb_epochs):
    nn_model.fit(np.asarray(X), np.asarray(y), shuffle=False, verbose=1, validation_data=(np.asarray(val_X), np.asarray(val_y)), batch_size=48, nb_epoch=1)
    nn_model.save_weights(experiment_path + '/nn_W',overwrite=True)

    model_dec = Sequential()
    model_dec.add(Dense_dec(500, input_shape=(1138,), activation='sigmoid',weights_path=experiment_path + '/nn_W'))
    dec_X_train = K.eval(model_dec(K.variable(np.asarray(X))))
    dec_X_val = K.eval(model_dec(K.variable(np.asarray(val_X))))

    dval = xgb.DMatrix(dec_X_val, label=val_y)
    dtrain = xgb.DMatrix(dec_X_train, label=y)
    watchlist = [(dval,'val'), (dtrain,'train')]
    model = xgb.train(params, dtrain, 200, watchlist)
    #model.save_model('weights_models/xgb.model')
